package com.jinbiao.quasar.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.jinbiao.quasar.annotate.OperationLog;
import com.jinbiao.quasar.model.SysJob;
import com.jinbiao.quasar.model.SysJobLog;
import com.jinbiao.quasar.scheduling.quartz.JobManager;
import com.jinbiao.quasar.util.AjaxJson;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.extend.sqltoy.annotation.Db;
import org.sagacity.sqltoy.dao.SqlToyLazyDao;
import org.sagacity.sqltoy.model.EntityQuery;
import org.sagacity.sqltoy.model.EntityUpdate;
import org.sagacity.sqltoy.model.Page;

import java.util.HashMap;
import java.util.List;

@Controller
@OperationLog
public class JobController {
    @Db
    SqlToyLazyDao dao;

    @Mapping("/job/get-job-list")
    public AjaxJson getJobList(int page, int pageSize, String sortBy, boolean desc, SysJob job) {
        EntityQuery entityQuery = EntityQuery.create().where("#[and job_name like :jobName] " +
                "            #[and job_inner_name like :jobInnerName] #[and job_group like :jobGroup]");
        Page<SysJob> pageResult = dao.findPageEntity(new Page(pageSize, page), SysJob.class, desc ? entityQuery.orderByDesc(sortBy) : entityQuery.orderBy(sortBy), SysJob.class);
        return AjaxJson.getSuccessData(pageResult);
    }

    @Mapping("/job/delete-job-by-id")
    public AjaxJson deleteJobById(SysJob sysJob) {
        List<SysJob> results = dao.findBySql("sysJobList", sysJob);
        if (CollectionUtil.isNotEmpty(results)) {
            SysJob job = results.get(0);
            JobManager.getInstance().jobRemove(job.getJobInnerName());
        }
        return AjaxJson.getSuccessData(dao.delete(sysJob));
    }

    @Mapping("/job/delete-log-by-id")
    public AjaxJson deleteJobById(SysJobLog sysJobLog) {
        return AjaxJson.getSuccessData(dao.delete(sysJobLog));
    }

    @Mapping("/job/edit-job")
    public AjaxJson editJob(SysJob sysJob) {
        if ("onOff_on".equals(sysJob.getStatus())) {
            JobManager.getInstance().jobRemove(sysJob.getJobInnerName());
            JobManager.getInstance().jobStart(sysJob.getJobInnerName(), new HashMap<>());
        }
        return AjaxJson.getSuccessData(dao.saveOrUpdate(sysJob));
    }

    @Mapping("/job/add-job")
    public AjaxJson addJob(SysJob sysJob) {
        dao.saveOrUpdate(sysJob);
        if ("onOff_on".equals(sysJob.getStatus())) {
            JobManager.getInstance().jobStart(sysJob.getJobInnerName(), new HashMap<>());
        }
        return AjaxJson.getSuccess();
    }

    @Mapping("/job/query-job-by-id")
    public AjaxJson queryJobById(SysJob sysJob) {
        List<SysJob> results = dao.findBySql("sysJobList", sysJob);
        if (CollectionUtil.isNotEmpty(results)) {
            SysJob job = results.get(0);
            return AjaxJson.getSuccessData(job);
        }
        return AjaxJson.getSuccessData(null);
    }

    @Mapping("/job/trigger-job")
    public AjaxJson triggerJob(SysJob sysJob) {
        List<SysJob> results = dao.findBySql("sysJobList", sysJob);
        if (CollectionUtil.isNotEmpty(results)) {
            SysJob job = results.get(0);
            JobManager.getInstance().triggerJob(job.getJobInnerName());
        }
        return AjaxJson.getSuccessData(null);
    }

    @Mapping("/job/stop-job")
    public AjaxJson stopJob(SysJob sysJob) {
        List<SysJob> results = dao.findBySql("sysJobList", sysJob);
        if (CollectionUtil.isNotEmpty(results)) {
            SysJob job = results.get(0);
            JobManager.getInstance().jobStop(job.getJobInnerName());
            dao.updateByQuery(SysJob.class, EntityUpdate.create().set("status", "onOff_off").where("id = ?").values(job.getId()));
        }
        return AjaxJson.getSuccessData(null);
    }

    @Mapping("/job/start-job")
    public AjaxJson startJob(SysJob sysJob) {
        List<SysJob> results = dao.findBySql("sysJobList", sysJob);
        if (CollectionUtil.isNotEmpty(results)) {
            SysJob job = results.get(0);
            JobManager.getInstance().jobStart(job.getJobInnerName(), new HashMap<>());
            dao.updateByQuery(SysJob.class, EntityUpdate.create().set("status", "onOff_on").where("id = ?").values(job.getId()));
        }
        return AjaxJson.getSuccessData(null);
    }

    @Mapping("/job/job-log")
    public AjaxJson jobLog(int page, int pageSize, String sortBy, boolean desc, SysJobLog sysJob) {
        Page<SysJobLog> pageResult = new Page<>();
        EntityQuery entityQuery = EntityQuery.create().where("job_inner_name = ? && job_group = ?").values(sysJob.getJobInnerName(),sysJob.getJobGroup());
        pageResult = dao.findPageEntity(new Page(pageSize, page), SysJobLog.class, desc ? entityQuery.orderByDesc(sortBy) : entityQuery.orderBy(sortBy));

        return AjaxJson.getSuccessData(pageResult);
    }

    @Mapping("/public/get-job-all")
    public AjaxJson getJobAll(SysJob sysJob) {
        return AjaxJson.getSuccessData(dao.findBySql("sysJobList", sysJob));
    }

}
